package com.dawnClass.service.impl;

import com.dawnClass.mapper.ConsumerMapper;
import com.dawnClass.pojo.Consumer;
import com.dawnClass.pojo.ConsumerExample;
import com.dawnClass.service.CodeService;
import com.dawnClass.util.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CodeServiceImpl implements CodeService {

    private static int codepassword;

    @Autowired
    private ConsumerMapper consumerMapper;
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String consumerEmail;

    public void setEmail(String consumerEmail) {
        this.consumerEmail = consumerEmail;
    }


    @Override
    public ServiceResponse getByEmail(String consumerEmail) {
        System.out.println("前端返回的邮箱："+consumerEmail);

        ConsumerExample example = new ConsumerExample();
        example.createCriteria().andConsumerEmailEqualTo(consumerEmail);
        List<Consumer> consumerList = consumerMapper.selectByExample(example);
        if (consumerList.size()>0){
            Consumer consumer = consumerList.get(0);
            String getConsumerEmail = consumer.getConsumerEmail();
            System.out.println("数据库读取到的邮箱:"+getConsumerEmail);
            if(consumerEmail.equals(getConsumerEmail)){
                SimpleMailMessage msg = new SimpleMailMessage();
                //msg.setFrom("2687211534@qq.com");//发送方 - 应该和application-dev.yml文件中的username保持一致
                msg.setFrom("2687211534@qq.com");//发送方 - 应该和application-dev.yml文件中的username保持一致

                msg.setSubject("苏鸿云课验证码");//标题

                //验证码是随机的10000 ~ 99999
                codepassword = (int) (Math.random()*90000+10000);
                msg.setText("验证码:"+codepassword);
                msg.setTo(consumerEmail);//登录页面输入的 - 收件方

                //真正的发送
                mailSender.send(msg);
                    return ServiceResponse.getSuccess(consumer);
            }
        }
        return ServiceResponse.getFail(null);
    }

    @Override
    public ServiceResponse getByEmailAndPassword(String consumerEmail, Integer password) {
        System.out.println("前端返回的邮箱:"+consumerEmail);
        System.out.println("前端返回邮箱验证码:"+password);
        ConsumerExample example = new ConsumerExample();
        example.createCriteria().andConsumerEmailEqualTo(consumerEmail);
        List<Consumer> consumerList = consumerMapper.selectByExample(example);
        if (consumerList.size()>0){
            Consumer consumer = consumerList.get(0);
            String getConsumerEmail = consumer.getConsumerEmail();
            System.out.println("数据库读取到的邮箱:"+getConsumerEmail);
                if(password.equals(codepassword)){
                    return ServiceResponse.getSuccess("Validation-succeeded");
                }
        }
        return ServiceResponse.getFail("Validation-failed");
    }
}
